package org.an.proj.contacts.net.service.retrofit.upload;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 上传拦截器
 */
public class UploadProgressInterceptor implements Interceptor {

  private ProgressListener progressListener;

  public UploadProgressInterceptor(ProgressListener progressListener) {
    this.progressListener = progressListener;
  }

  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    if (request.body() == null) {
      return chain.proceed(request);
    }

    Request progressRequest = request.newBuilder()
        .method(request.method(), new ProgressRequestBody(request.body(), progressListener))
        .build();

    return chain.proceed(progressRequest);
  }
}
